package com.anyou.mall.sho.service.impl;

import java.util.List;

import com.anyou.common.exception.GlobalException;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.fin.domain.StoreWithdrawalAduit;
import com.anyou.mall.fin.service.IStoreWithdrawalAduitService;
import com.anyou.mall.goo.goods.domain.GooGoods;
import com.anyou.mall.goo.goods.service.IGooGoodsService;
import com.anyou.mall.sho.domain.CarPartsShop;
import com.anyou.mall.sho.service.ICarPartsShopService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.sho.mapper.ShopNoticeMapper;
import com.anyou.mall.sho.domain.ShopNotice;
import com.anyou.mall.sho.service.IShopNoticeService;

/**
 * 商家通知信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-30
 */
@Service
public class ShopNoticeServiceImpl extends ServiceImpl<ShopNoticeMapper, ShopNotice> implements IShopNoticeService
{
    @Autowired
    private ShopNoticeMapper shopNoticeMapper;
    @Autowired
    private ICarPartsShopService carPartsShopService;
    @Autowired
    private IGooGoodsService gooGoodsService;
    @Autowired
    private IStoreWithdrawalAduitService storeWithdrawalAduitService;


    /**
     * 查询商家通知信息
     * 
     * @param shopNoticeId 商家通知信息主键
     * @return 商家通知信息
     */
    @Override
    public ShopNotice selectShopNoticeByShopNoticeId(Long shopNoticeId)
    {
        return shopNoticeMapper.selectShopNoticeByShopNoticeId(shopNoticeId);
    }

    /**
     * 查询商家通知信息列表
     * 
     * @param shopNotice 商家通知信息
     * @return 商家通知信息
     */
    @Override
    public List<ShopNotice> selectShopNoticeList(ShopNotice shopNotice)
    {
        return shopNoticeMapper.selectShopNoticeList(shopNotice);
    }

    /**
     * 新增商家通知信息
     * 
     * @param shopNotice 商家通知信息
     * @return 结果
     */
    @Override
    public int insertShopNotice(ShopNotice shopNotice)
    {
        shopNotice.setCreateTime(DateUtils.getNowDate());
        return shopNoticeMapper.insertShopNotice(shopNotice);
    }

    /**
     * 修改商家通知信息
     * 
     * @param shopNotice 商家通知信息
     * @return 结果
     */
    @Override
    public int updateShopNotice(ShopNotice shopNotice)
    {
        shopNotice.setUpdateTime(DateUtils.getNowDate());
        return shopNoticeMapper.updateShopNotice(shopNotice);
    }

    /**
     * 批量删除商家通知信息
     * 
     * @param shopNoticeIds 需要删除的商家通知信息主键
     * @return 结果
     */
    @Override
    public int deleteShopNoticeByShopNoticeIds(Long[] shopNoticeIds)
    {
        return shopNoticeMapper.deleteShopNoticeByShopNoticeIds(shopNoticeIds);
    }

    /**
     * 删除商家通知信息信息
     * 
     * @param shopNoticeId 商家通知信息主键
     * @return 结果
     */
    @Override
    public int deleteShopNoticeByShopNoticeId(Long shopNoticeId)
    {
        return shopNoticeMapper.deleteShopNoticeByShopNoticeId(shopNoticeId);
    }
    /**
     * 新增商家系统消息
     * @param systemType 系统通知类型(1-商品,2-提现,3-认证)
     * @param msgContent 消息内容
     * @param sourceId 关联的资源id,类型为3时关联carPartsShopAuditId,类型为1时关联goodsId,类型为2时关联withdrawalId
     */
    @Override
    public void insertShopSystemNotice(int systemType, String msgContent, Long sourceId) {
        ShopNotice shopNotice = new ShopNotice();
        shopNotice.setNoticeType(0);
        switch (systemType) {
            case 1: //商品
                //查询汽配商
                GooGoods gooGoods = gooGoodsService.getById(sourceId);
                if(gooGoods==null){
                    throw new GlobalException("商品不存在");
                }
                shopNotice.setCarPartsShopId(gooGoods.getStoreId());
                shopNotice.setSystemType(1);
                shopNotice.setNoticeContent(msgContent);
                shopNotice.setGoodsId(sourceId);
                break;
            case 2: //提现
                //查询汽配商
                StoreWithdrawalAduit swa = storeWithdrawalAduitService.getById(sourceId);
                if(swa==null){
                    throw new GlobalException("提现申请不存在");
                }
                shopNotice.setCarPartsShopId(swa.getCarPartsShopId());
                shopNotice.setSystemType(2);
                shopNotice.setNoticeContent(msgContent);
                shopNotice.setWithdrawalId(sourceId);
                break;
            case 3: //认证
                //查询汽配商
                CarPartsShop one = carPartsShopService.getOne(new LambdaQueryWrapper<CarPartsShop>()
                        .eq(CarPartsShop::getCarPartsShopAuditId, sourceId)
                        .eq(CarPartsShop::getDelFlag, "0"));
                if (one == null) {
                    throw new GlobalException("汽配商不存在");
                }
                shopNotice.setCarPartsShopId(one.getCarPartsShopId());
                shopNotice.setSystemType(3);
                shopNotice.setNoticeContent(msgContent);
                shopNotice.setCarPartsShopAuditId(sourceId);
                break;
        }
        int insert = shopNoticeMapper.insert(shopNotice);
    }
}
